package cn.jdy.plugins.admin;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Maid;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.service.MaidService;
import cn.jdy.plugins.service.MemberService;


/**
 * @title 商家管理后台操作
 * @author jga
 * @since 1-12-2015
 */
@Controller
@RequestMapping("/maid")
public class MaidCtrl extends BaseCtrl{
	@Autowired
	private MaidService maidService;
	@Autowired
	private ConfService confService;
	//默认列表路径
	private static String viewName = "redirect:/maid/list?s=";
	@Autowired
	private MemberService memberService;
	/**
	 *  初始化
	 * @param entity
	 * @param fromDate
	 * @param toDate
	 * @param s
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(Maid entity, String type,String fromDate, String toDate, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/maid/maidList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
		
		mv.addObject("type", type);
		   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");//设置日期格式
			mv.addObject("time", df.format(new Date()));
		mv.addObject("entity", entity);
		return mv;
	}
	
	/**
	 * 异步查询
	 * @param sp
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(Maid sp){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", maidService.query(sp));
			jsonMap.put("iTotalDisplayRecords", maidService.count(sp));
			jsonMap.put("iTotalRecords", maidService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}
	
	/**
	 * 异步查询
	 * @param sp
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	@RequestMapping("/amain")
	@ResponseBody
	public Map<String, Object> amain(Maid sp){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");//设置日期格式
				String beyue= df.format(new Date());//本月
			List<Maid> list =	maidService.querybenyuemai(sp.getCreateTime(),sp.getMch_id());
			
			for (Maid maid : list) {
				Member	member=memberService.find(maid.getWermemberid());
				//money.
				member.setMoney(member.getMoney()+maid.getFendtotal());
				maid.setIsfa("1");
				maidService.update(maid);
				
			}
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}
	
	 
 
 
	
}
